package com.orhon.smartcampus.modules.base.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudent;
import com.orhon.smartcampus.modules.student.mm.StudentMapper;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.UserTypeUtil;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class SchoolsFetcher implements DataFetcher {

	@Autowired
	SchoolsDao schoolsDao;
	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		List<GQLSchool> schools = new ArrayList<>();
		String token = tokenService.getSystemToken();
		String hql = "from Schools as school where 1=1 " + UserTypeUtil.decideId(token);

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("deleted_at")!=null) {
				hql += " and school.deleted_at  " + Util.decide(map.get("deleted_at"));
			}
			if(map.get("school_type")!=null) {
				hql += " and school.school_type "+ Util.decide(map.get("school_type"));
			}
			if(map.get("city_id")!=null) {
				hql += " and school.city_id "+ Util.decide(map.get("city_id"));
			}
			if(map.get("region_id")!=null) {
				hql += " and school.region_id "+ Util.decide(map.get("region_id"));
			}
			if(map.get("cloud_status")!=null) {
				hql += " and school.cloud_status "+ Util.decide(map.get("cloud_status"));
			}
		}
System.out.println(hql);
		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		List<Schools> list = schoolsDao.find(hql,null,page.getPage(),page.getLimit());

		for (Schools item : list) {
			GQLSchool parGQLSchool = null;
			if (env.getSelectionSet().contains("data/parent")){
				Schools schools2 = item.getSchools();
				parGQLSchool = SchoolMapper.MAPPER.toSchool(schools2);
			}

			List<GQLModule> gqlModule = null;
			if (env.getSelectionSet().contains("data/modules")){
				List<Modules> modules = item.getModules();
				gqlModule = ModuleMapper.MAPPER.mapModuleList(modules);
			}

			List<GQLUser> gqlUser = null;
			if (env.getSelectionSet().contains("data/users")){
				List<Users> users = item.getUsers();
				gqlUser = UserMapper.MAPPER.mapList(users);
			}

			List<GQLStudent> gqlStudent = null;
			if (env.getSelectionSet().contains("data/students")){
				List<SInformation> stulist = item.getStudents();
				gqlStudent = StudentMapper.MAPPER.toStudentList(stulist);
			}

			List<GQLTeacher> gqlTeacher = null;
			if (env.getSelectionSet().contains("data/teachers")){
				List<TInformation> teachers = item.getTeachers();
				gqlTeacher = TeacherMapper.MAPPER.toTeacherList(teachers);
			}
			//			SchooltypesDeleateable schoolType = null;
			//			if (env.getSelectionSet().contains("teachers")){
			//				List<TInformation> teachers = item.getTeachers();
			//				gqlTeacher = TeacherMapper.MAPPER.toTeacherList(teachers);
			//			}
			GQLSchool school = SchoolMapper.MAPPER.toSchool(item);
			school.setParent(parGQLSchool);
			school.setModules(gqlModule);
			school.setUsers(gqlUser);
			school.setStudents(gqlStudent);
			school.setTeachers(gqlTeacher);
			schools.add(school);
		}

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = schoolsDao.getTotalCount(hql);
		}
		return Result.ok().put("data", schools).put("total", total);
	}


}
